package com.yanqu.road.dao.impl.player;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserActivityScore;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/10 15:58
 */
public class UserActivityScoreDaoImpl extends TempDataObjectDao<UserActivityScore> {

    public Map<Integer, Map<Integer, UserActivityScore>> getUserActivityScoreMap(long userId) {
        Map<Integer, Map<Integer, UserActivityScore>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_score where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityScore data = parse(rs);
                if (!dataMap.containsKey(data.getActivityId())) {
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getActivityId()).put(data.getGoodsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserActivityScore data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_activity_score(activityId,activityType,userId,goodsId,dayScore,totalScore,lastResetTime) VALUES(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getActivityType());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getGoodsId());
            call.setLong(i++, data.getDayScore());
            call.setLong(i++, data.getTotalScore());
            call.setLong(i++, data.getLastResetTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserActivityScore data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_activity_score SET dayScore=?,totalScore=?,lastResetTime=? WHERE goodsId=? and userId=? and activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getDayScore());
            call.setLong(i++, data.getTotalScore());
            call.setLong(i++, data.getLastResetTime());
            call.setInt(i++, data.getGoodsId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserActivityScore parseData(ResultSet rs) throws SQLException {
        UserActivityScore data = new UserActivityScore();
        data.setActivityId(rs.getInt("activityId"));
        data.setActivityType(rs.getInt("activityType"));
        data.setUserId(rs.getLong("userId"));
        data.setGoodsId(rs.getInt("goodsId"));
        data.setDayScore(rs.getLong("dayScore"));
        data.setTotalScore(rs.getLong("totalScore"));
        data.setLastResetTime(rs.getLong("lastResetTime"));
        return data;
    }
}
